//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FloatingRateCouponPricer.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CFloatingRateCouponPricer::CFloatingRateCouponPricer (boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFloatingRateCouponPricer = NULL;
#endif
	_ppFloatingRateCouponPricer = &childNative;
    
}
Cephei::QL::Cashflows::CFloatingRateCouponPricer::CFloatingRateCouponPricer (QuantLib::FloatingRateCouponPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFloatingRateCouponPricer = NULL;
#endif
	_ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (&childNative);
    
    _FloatingRateCouponPricerOwner = owner;
    
}

Cephei::QL::Cashflows::CFloatingRateCouponPricer::CFloatingRateCouponPricer (CFloatingRateCouponPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFloatingRateCouponPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CFloatingRateCouponPricer::CFloatingRateCouponPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFloatingRateCouponPricer = NULL;
#endif
	if (!t->IsSubclassOf(CFloatingRateCouponPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CFloatingRateCouponPricer::CFloatingRateCouponPricer (QuantLib::Handle<QuantLib::FloatingRateCouponPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFloatingRateCouponPricer = &childNative;
	_ppFloatingRateCouponPricer = &static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>>(childNative.currentLink());
    
    _FloatingRateCouponPricerOwner = owner;
}
Cephei::QL::Cashflows::CFloatingRateCouponPricer::CFloatingRateCouponPricer (QuantLib::Handle<QuantLib::FloatingRateCouponPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFloatingRateCouponPricer = &childNative;
	_ppFloatingRateCouponPricer = &static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CFloatingRateCouponPricer::CFloatingRateCouponPricer (QuantLib::FloatingRateCouponPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFloatingRateCouponPricer = NULL;
#endif
	_ppFloatingRateCouponPricer = new boost::shared_ptr<QuantLib::FloatingRateCouponPricer> (new QuantLib::FloatingRateCouponPricer (childNative));
    
}
#endif

Cephei::QL::Cashflows::CFloatingRateCouponPricer::~CFloatingRateCouponPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFloatingRateCouponPricer != NULL)
    {
	    delete _ppFloatingRateCouponPricer;
        _ppFloatingRateCouponPricer = NULL;
    }
}
Cephei::QL::Cashflows::CFloatingRateCouponPricer::!CFloatingRateCouponPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFloatingRateCouponPricer != NULL)
    {
	    delete _ppFloatingRateCouponPricer;
    }
}
QuantLib::FloatingRateCouponPricer& Cephei::QL::Cashflows::CFloatingRateCouponPricer::GetReference ()
{
    if (_ppFloatingRateCouponPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppFloatingRateCouponPricer;
}
boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& Cephei::QL::Cashflows::CFloatingRateCouponPricer::GetShared ()
{
    if (_ppFloatingRateCouponPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppFloatingRateCouponPricer;
}
QuantLib::FloatingRateCouponPricer* Cephei::QL::Cashflows::CFloatingRateCouponPricer::GetPointer ()
{
    if (_ppFloatingRateCouponPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppFloatingRateCouponPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FloatingRateCouponPricer>& Cephei::QL::Cashflows::CFloatingRateCouponPricer::GetHandle ()
{
	if (_phFloatingRateCouponPricer == NULL)
	{
		_phFloatingRateCouponPricer = new Handle<QuantLib::FloatingRateCouponPricer> (*_ppFloatingRateCouponPricer);
	}
	return *_phFloatingRateCouponPricer;
}
#endif
bool Cephei::QL::Cashflows::CFloatingRateCouponPricer::HasNative () 
{
	return (_ppFloatingRateCouponPricer != NULL);
}

Double Cephei::QL::Cashflows::CFloatingRateCouponPricer::CapletPrice (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFloatingRateCouponPricer)->capletPrice ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFloatingRateCouponPricer::CapletRate (Double effectiveCap)
{
    try
    {
        QuantLib::Rate _effectiveCap = (QuantLib::Rate)ValueHelper::Convert (effectiveCap); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppFloatingRateCouponPricer)->capletRate ( _effectiveCap );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFloatingRateCouponPricer::FloorletPrice (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFloatingRateCouponPricer)->floorletPrice ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFloatingRateCouponPricer::FloorletRate (Double effectiveFloor)
{
    try
    {
        QuantLib::Rate _effectiveFloor = (QuantLib::Rate)ValueHelper::Convert (effectiveFloor); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppFloatingRateCouponPricer)->floorletRate ( _effectiveFloor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IFloatingRateCouponPricer^ Cephei::QL::Cashflows::CFloatingRateCouponPricer::Initialize (Cephei::QL::Cashflows::IFloatingRateCoupon^ coupon)
{
    CFloatingRateCoupon^ _Ccoupon;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccoupon = safe_cast<CFloatingRateCoupon^> (coupon);
        _Ccoupon->Lock();
        QuantLib::FloatingRateCoupon& _coupon = static_cast<QuantLib::FloatingRateCoupon&> (_Ccoupon->GetReference ()); 
    	(*_ppFloatingRateCouponPricer)->initialize ( _coupon );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccoupon != nullptr) _Ccoupon->Unlock();
    }
}
Double Cephei::QL::Cashflows::CFloatingRateCouponPricer::SwapletPrice::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFloatingRateCouponPricer)->swapletPrice ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFloatingRateCouponPricer::SwapletRate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppFloatingRateCouponPricer)->swapletRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::IFloatingRateCouponPricer^ Cephei::QL::Cashflows::CFloatingRateCouponPricer::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppFloatingRateCouponPricer)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

